package com.lazy.book.common.entity;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.lazy.book.common.enums.CommonResponseEnum;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
@Data
public class ApiResult<T> implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;


  public String code = "0000";

  public String message = "SUCCESS";

  public T data;

  private Object param = new Object();

  public ApiResult() {
  }

  public ApiResult(String code, String message) {
    this.code = code;
    this.message = message;
  }

  public static <T> ApiResult<T> success() {
    ApiResult<T> apiResult = new ApiResult<>();
    apiResult.data = null;
    return apiResult;
  }

  public static <T> ApiResult<T> success(T data) {
    ApiResult<T> apiResult = new ApiResult<>();
    apiResult.data = data;
    return apiResult;
  }

  public static <T> ApiResult<T> success(T data, Object param) {
    ApiResult<T> apiResult = new ApiResult<>();
    apiResult.data = data;
    if (ObjectUtil.isNotNull(param)) {
      apiResult.param = param;
    }
    return apiResult;
  }

  public static <T> ApiResult<T> error(CommonResponseEnum responseEnum) {
    ApiResult<T> apiResult = new ApiResult<>();
    apiResult.code = getResponseCode(responseEnum);
    apiResult.message = responseEnum.getMessage();
    apiResult.data = null;
    return apiResult;
  }

  protected static String getResponseCode(CommonResponseEnum responseEnum) {
    return responseEnum.getCodePrefixEnum().getPrefix() + responseEnum.getCode();
  }

}
